<?php

namespace App\model\broker;

use Illuminate\Database\Eloquent\Model;
use phpDocumentor\Reflection\Types\Self_;
use App\Repository\ShareRepository;
use Illuminate\Support\Carbon;
/**
 * App\model\broker\BrokerCustomer
 *
 * @property int $id
 * @property int $first_id 直推id
 * @property int $second_id 二级id
 * @property int $user_id 用户id
 * @property int $project_id 项目id
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property string $status 状态
 * @property string $effective_time 切换有效的时间
 * @property string|null $follow_time 继续跟进中的时间
 * @property string|null $transaction_time 成交时间
 * @property float|null $effective_first_reward 有效客户奖励直推奖励
 * @property float|null $effective_share_reward 有效分享奖励
 * @property float|null $follow_first_reward 跟进直推奖励
 * @property float|null $follow_share_reward 跟进分享奖励
 * @property float|null $transaction_first_reward 成交直推奖励
 * @property float|null $transaction_share_reward 成交分享奖励
 * @property float|null $second_reward 直推上级奖励 按比例计算
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereEffectiveFirstReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereEffectiveShareReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereEffectiveTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereFirstId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereFollowFirstReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereFollowShareReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereFollowTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereProjectId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereSecondId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereSecondReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereTransactionFirstReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereTransactionShareReward($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereTransactionTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\broker\BrokerCustomer whereUserId($value)
 * @mixin \Eloquent
 */


class BrokerCustomer extends Model
{
    protected  $table='broker_customer'; //绑定关系表
    const  INVALiD='invalid';
    const  EFFECTIVE='effective';
    const FOLLOW='follow';
    const TRANSACTION='transaction';
    const  UNINTENTIONAL='Unintentional';
    const REVIEW='review';
    static $statusMap=[
        self::REVIEW=>'待审核',
        self::INVALiD=>'无效',
        self::EFFECTIVE=>'有效跟进中',
        self::FOLLOW=>'继续跟进中',
        self::TRANSACTION=>'成交',
        self::UNINTENTIONAL=>'无意向'
    ];
    public static function boot()
    {
        parent::boot();
        static::saving(function ($model) {
            if($model->status==BrokerCustomer::EFFECTIVE){
                $model->effective_time=Carbon::now();
            }else if($model->status==BrokerCustomer::FOLLOW){
                $model->follow_time=Carbon::now();
            }else if($model->status==BrokerCustomer::TRANSACTION){
                $model->transaction_time=Carbon::now();
            }
        });
    }
    public function setStatusAttribute($value){
        if(strcmp($value,$this->status)!==0&&$value != BrokerCustomer::REVIEW){
            $shareRepository = new ShareRepository();
            $shareRepository->changeStatus($this,$value);
        }
        $this->attributes['status']=$value;

    }
}
